#coding:utf-8
# Create your views here.
from django.core import serializers
from auth.models import Token
from django.views.decorators.csrf import csrf_exempt
import json
from django.http import HttpResponse
from auth.utils import json_response,token_required
from media.models import Category, Media

@csrf_exempt
@token_required
def media(request):

    if request.method == 'GET':
        categoryName = request.GET.get('category')
        if categoryName is None:
            return json_response({
                'error':'lack of category param'
            },status=400)
        category = Category.objects.filter(name=categoryName)
        if category is None:
            return json_response({
                'error':'Invalid category'
            },status=400)
        media = serializers.serialize('json',Media.objects.filter(category=category))

        # print(media)

        return json_response(media)

    elif request.method == 'OPTIONS':
        return json_response({})
    else:
        return json_response({
            'error': 'Invalid Method'
        }, status=405)